/* $Header: sequence.h,v 2.2 01/10/02 10:30:39 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  sequence.h                                      */
/* ==================                                                  */
/*                                                                     */
/* Author:  Tom Przybylski                                             */
/*                                                                     */
/* COMMENT:  Defines the data structures, constants, and macros used   */
/*           for sequences                                             */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef SEQ_H
#define SEQ_H

/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/

#define SEQ_VERSION      1    /* version of sequence format             */
#define MAX_SUBSEQ       6    /* max number of subsequences in sequence */
#define PRIORITY_SEQ     5    /* subseq# of priority sequence (last subseq) */
#define NOT_SUBSEQ       6    /* Lowest invalid subseq index            */
#define MAX_SEQ          5    /* max number stored sequences            */
#define POST_SEQ        99    /* marker for post sequence lines in kbd table */

#define METHOD_OFF    0x80    /* method # for OFF and method = 0        */
                              /*  (OFF = Sign bit set                   */

#define MAX_VALVE_INJ    99
#define MAX_VALVE_REPEAT 99
#define MAX_ALS_INJ      99
#define MAX_ALS_BOTTLE   999  /* Biggest allowed bottle#; tray, tower may limit */

/*****************************/
/*   MACROS                  */
/*****************************/


/*****************************/
/*   TYPEDEFS                */
/*****************************/

typedef enum
{
   UNDEFINED_SEQ = 0,
   F_ALS_SEQ     = 1,
   B_ALS_SEQ     = 2,
   BOTH_ALS_SEQ  = 3,
   VALVE_SEQ     = 4

} SEQ_TYPE;


typedef struct
{
   U_INT8  num_inj;
   U_INT16 begin_range;
   U_INT16 end_range;

} INJ_SEQ;


typedef struct
{
   U_INT8     method;
   SEQ_TYPE   type;
   U_INT8     repeat;         /* for Valves */
   INJ_SEQ    front;          /* for ALS, Valves */
   INJ_SEQ    back;           /* for ALS    */

} SUBSEQ;


typedef struct
{
   DATETIME   datetime;
   U_INT8     version;
   BIT8       priority;
   U_INT8     post_seq_method;
   BIT8       repeat_seq;

} SEQ_HEADER;


typedef struct
{
   SEQ_HEADER  header;
   SUBSEQ      subseq[MAX_SUBSEQ];

} SEQUENCE, *pSEQUENCE;


typedef struct
{
   U_INT8    subseq;
   U_INT8    method;
   SEQ_TYPE  type;
   U_INT8    rep_num;
   U_INT16   f_sample;
   U_INT16   b_sample;
   BIT8      f_active;
   BIT8      b_active;
   U_INT8    f_inj_num;
   U_INT8    b_inj_num;

} SEQ_CTRL;


typedef enum
{
   SEQ_STOPPED      = 0,
   SEQ_PAUSED       = 1,
   SEQ_RUNNING      = 2,
   SEQ_STEPPING     = 3,
   SEQ_PF_HALT      = 4,   /* INET power fail restart */
   SEQ_ABORT        = 5

} SEQ_STATE;


typedef enum
{
   SEQ_ALS_WAIT      = 0,
   SEQ_STEP          = 1,
   SEQ_BOTTLE_RETURN = 2,
   SEQ_WAIT          = 3,
   SEQ_VALVE_INJ     = 4,
   SEQ_SYNC          = 5,
   SEQ_RUN           = 6,
   SEQ_STEP_WAIT     = 7

} SEQ_SUBSTATE;


typedef enum
{
   GSV_INJ_IDLE      = 0,
   GSV_READY_WAIT    = 1,
   GSV_PREP_RUN      = 2,
   GSV_IDLE_ABORT    = 3,      /* STOP Key pressed */
   GSV_NO_RDY_WAIT   = 4,   /* for petro chem PC */
   GSV_NO_RDY_RUN    = 5    /* for petro chem PC */

} GSV_INJ_STATE;


/****************************/
/*   EXTERNAL VARIABLES     */
/****************************/

#pragma SECTION DATA=pfail

extern  SEQ_STATE    Seq_State;
extern  SEQ_SUBSTATE Seq_Substate;
extern  BIT8         Priority_In_Progress;
extern  BIT8         Load_Post_Seq_Method;
extern  BIT8         Overlap_Seq;

extern  INT16        Front_Sample;
extern  INT16        Back_Sample;
extern  INT16        Front_Sample_Inj;
extern  INT16        Back_Sample_Inj;

extern  SEQ_CTRL     Seq_Current;
extern  SEQ_CTRL     Seq_Last;
extern  SEQ_CTRL     Seq_Hold;

extern  SEQUENCE     Active_Seq;

/* Stored_Seq[0] is the "check area" used in sequence loading.   */
/* Stored_Seq[1] ... [n] are used for storage.                   */

extern  SEQUENCE     Stored_Seq[];

#pragma SECTION UNDO


extern  RESOURCE  Seq_Resource;
extern  SEQ_CTRL  Seq_Next;
extern  UI_ERR    Seq_Parm_Err;
extern  INT8      Seq_Parm;
extern  BIT8      Return_Bottles;
extern  BIT8      First_Priority_Sample;
extern  U_INT8    Seq_Left;
extern  U_INT8    Seq_Right;
extern  U_INT16   Seq_Line;

extern  INT32     Last_Inj_Abort;
extern  BIT8      Start_Has_Occurred;
extern  BIT8      Injection_Complete;
extern  BIT8      Ignore_Ready;

extern  U_INT8         Gsv_Inj_1;
extern  U_INT8         Gsv_Inj_2;
extern  GSV_INJ_STATE  Gsv_Inj_State;


/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

BIT8 AnySeqLeft (void);

void InitializeSequences (void);
void HandleSequence (void);
void RewindSequence (void);
BIT8 PrepareForSeqStep (void);
void RecoverSequence (void);
void RecoveryStopSequence (void);

void EraseSequence (pSEQUENCE seq);

SEQ_STATE GetSequenceState (void);

void HandleWorkstationGsvInjection (void);

#endif
